package com.controller.order;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.base.GetBaseCarInfoDO;
import com.model.domain.order.GetOrderRepairRecordInfoDO;
import com.model.dto.base.car.GetCarListDTO;
import com.model.dto.order.repair.GetRecordAllDTO;
import com.model.dto.order.repair.GetRecordListDTO;
import com.model.pojo.base.BaseCarInfo;
import com.model.pojo.order.OrderRepairRecordInfo;
import com.model.vo.base.car.getCarList.GetCarListVO;
import com.model.vo.order.repair.getRecordList.GetRecordListVO;
import com.service.base.BaseCarInfoService;
import com.service.order.OrderRepairRecordInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-09-09 15:43
 **/
@Api(tags = {"维修订单管理"})
@Slf4j
@RestController
@RequestMapping("/v1/order/repair")
@RequiredArgsConstructor
public class RepairRecordInfoController {
    private final OrderRepairRecordInfoService orderRepairRecordInfoService;
    @Log(title = "维修订单管理-记录列表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "记录列表")
    @PostMapping("/getRecordList")
    public Result<PageResult<List<GetRecordListVO>>> getRecordList(@RequestBody @Valid GetRecordListDTO dto){
        List<GetRecordListVO> voList = new ArrayList<>();
        GetOrderRepairRecordInfoDO getOrderRepairRecordInfoDO = EntityConversionUtil.copy(dto,GetOrderRepairRecordInfoDO.class);
        if(StringUtils.isNotEmpty(getOrderRepairRecordInfoDO.getProvince())){
            String province = getOrderRepairRecordInfoDO.getProvince().replace("省", "");
            getOrderRepairRecordInfoDO.setProvince(province);
        }
        List<OrderRepairRecordInfo> list = orderRepairRecordInfoService.getPageList(getOrderRepairRecordInfoDO);
        voList = EntityConversionUtil.copyList(list,GetRecordListVO.class);
        PageInfo<OrderRepairRecordInfo> pageInfo = new PageInfo<>(list);
        PageResult<List<GetRecordListVO>> build = PageResult.<List<GetRecordListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "维修订单管理-记录所有数据", businessType = BusinessType.INSERT)
    @ApiOperation(value = "记录所有数据")
    @PostMapping("/getRecordAll")
    public Result<List<GetRecordListVO>> getRecordAll(@RequestBody @Valid GetRecordAllDTO dto){
        GetOrderRepairRecordInfoDO getOrderRepairRecordInfoDO = EntityConversionUtil.copy(dto,GetOrderRepairRecordInfoDO.class);
        if(StringUtils.isNotEmpty(getOrderRepairRecordInfoDO.getProvince())){
            String province = getOrderRepairRecordInfoDO.getProvince().replace("省", "");
            getOrderRepairRecordInfoDO.setProvince(province);
        }
        List<OrderRepairRecordInfo> list = orderRepairRecordInfoService.getList(getOrderRepairRecordInfoDO);
        List<GetRecordListVO> voList = EntityConversionUtil.copyList(list,GetRecordListVO.class);
        if(CollectionUtils.isNotEmpty(voList)){
            voList=voList.subList(0,voList.size()>20?20:voList.size()-1);
        }
        return Result.suc(voList);
    }
}
